﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Linq;

namespace Marmat.Forms.Views
{
    namespace DataGrid
    {
        public class RowCollection : IList
        {
            private Forms.DataGrid DataGrid;
            internal DataRow[] DataRowCollection;

            internal RowCollection(Forms.DataGrid DataGrid, DataRow[] Collection)
            {
                this.DataGrid = DataGrid;
                this.DataRowCollection = Collection;
            }

            /// <summary>
            /// 返回总的行数
            /// </summary>
            /// <value></value>
            /// <returns></returns>
            /// <remarks></remarks>
            public int Count
            {
                get
                {
                    return this.DataRowCollection.Count();
                }
            }

            /// <summary>
            /// 在Table中增加一行或多行，并返回所增加的第一行。
            /// </summary>
            /// <returns></returns>
            /// <remarks></remarks>
            public Row AddNew()
            {
                return this.AddNew(1);
            }

            /// <summary>
            /// 在Table中增加一行或多行，并返回所增加的第一行。
            /// </summary>
            /// <param name="count"></param>
            /// <returns></returns>
            /// <remarks></remarks>
            public Row AddNew(int count)
            {
                Row Item = null;
                var EventArgs = new DataGridCancelEventArgs();
                EventArgs._GridControl = this.DataGrid;
                this.DataGrid.RaiseEventBeforeAddDataRow(EventArgs);
                if (EventArgs.Cancel == false)
                {
                    for (int i = 1; i <= count; i++)
                    {
                        var DataRow = this.DataGrid.DataSource.NewRow();
                        var dr = new Row(this.DataGrid, DataRow);
                        var DataRowEventArgs = new DataRowEventArgs() { _GridControl = this.DataGrid, _Row = dr };
                        this.DataGrid.RaiseEventDataRowAdding(DataRowEventArgs);
                        this.DataGrid.DataSource.Rows.Add(dr.BaseRow);
                        this.DataGrid.RaiseEventDataRowAdded(DataRowEventArgs);
                        if (Item is null)
                            Item = dr;
                    }

                    this.DataGrid.Position = Item.Index;
                }

                return Item;
            }

            /// <summary>
            /// 清除所有行的集合
            /// </summary>
            /// <remarks></remarks>
            public void Clear()
            {
                foreach (DataRow dr in this.DataRowCollection)
                    dr.Delete();
            }

            bool IList.Contains(object value)
            {
                return false;
            }

            public Row this[int index]
            {
                get
                {
                    var dr = this.DataRowCollection[index];
                    var row = new Row(this.DataGrid, dr);
                    return row;
                }
            }

            object IList.this[int index]
            {
                get
                {
                    var dr = this.DataRowCollection[index];
                    var Row = new Row(this.DataGrid, dr);
                    return Row;
                }
                set
                {
                    throw new NotImplementedException();
                }
            }


            /// <summary>
            /// 删除行。
            /// </summary>
            /// <param name="row"></param>
            /// <returns></returns>
            /// <remarks></remarks>
            public bool Delete(Row row)
            {
                bool Result = false;
                var EventArgs = new DataRowCancelEventArgs();
                EventArgs._DataGrid = this.DataGrid;
                EventArgs._Row = row;
                if (row.BaseRow is object)
                {
                    this.DataGrid.RaiseEventBeforeDeleteDataRow(EventArgs);
                    if (EventArgs.Cancel == false)
                    {
                        var EventArgs_1 = new DataRowEventArgs();
                        EventArgs_1._GridControl = this.DataGrid;
                        EventArgs_1._Row = row;
                        this.DataGrid.RaiseEventDataRowDeleting(EventArgs_1);
                        row.BaseRow.Delete();
                        Result = true;
                        this.DataGrid.RaiseEventDataRowDeleted(EventArgs_1);
                    }
                }

                return Result;
            }
            /// <summary>
            /// 移除行
            /// </summary>
            /// <param name="row"></param>
            /// <remarks></remarks>
            public void Remove(Row row)
            {
                row.Delete();
            }

            /* TODO ERROR: Skipped RegionDirectiveTrivia */
            int IList.Add(object value)
            {
                return -1;
            }

            bool ICollection.IsSynchronized
            {
                get
                {
                    return false;
                }
            }

            object ICollection.SyncRoot
            {
                get
                {
                    return this;
                }
            }

            IEnumerator IEnumerable.GetEnumerator()
            {
                var items = new List<Row>();
                foreach (DataRow dr in this.DataRowCollection)
                {
                    var row = new Row(this.DataGrid, dr);
                    items.Add(row);
                }

                return items.ToArray().GetEnumerator();
            }

            void ICollection.CopyTo(Array array, int index)
            {
            }

            int IList.IndexOf(object value)
            {
                return -1;
            }

            void IList.Insert(int index, object value)
            {
            }

            bool IList.IsFixedSize
            {
                get
                {
                    return false;
                }
            }

            bool IList.IsReadOnly
            {
                get
                {
                    return false;
                }
            }

            void IList.Remove(object value)
            {
            }

            void IList.RemoveAt(int index)
            {
            }
            /* TODO ERROR: Skipped EndRegionDirectiveTrivia */
        }
    }
}